package com.wlk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlk.mapper.AccountingMonthMapper;
import com.wlk.mapper.AccountingYearMapper;
import com.wlk.mapper.ProjectMapper;
import com.wlk.mapper.ReimbMapper;
import com.wlk.pojo.Reimb;
import com.wlk.pojo.dto.AddReimbDTO;
import com.wlk.pojo.dto.ReimbDTO;
import com.wlk.pojo.dto.ReimbVerifyDTO;
import com.wlk.service.ReimbService;
import com.wlk.utils.OrderNumberGenerator;
import com.wlk.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
@Service
public class ReimbServiceImpl extends ServiceImpl<ReimbMapper, Reimb> implements ReimbService {
    @Resource
    private ReimbMapper reimbMapper;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private AccountingYearMapper accountingYearMapper;
    @Resource
    private AccountingMonthMapper accountingMonthMapper;

    @Override
    public Result addReimb(AddReimbDTO addReimbDTO) {
        // 创建一个对象
        Reimb reimb = new Reimb();
        //验证传递的对象中参数不为空
        String proCode = addReimbDTO.getProCode();
        String proName = addReimbDTO.getProName();
        BigDecimal reiMoney = addReimbDTO.getReiMoney();
        String reiReason = addReimbDTO.getReiReason();
        Integer reiApplicantId = addReimbDTO.getReiApplicantId();
        System.out.println("addReimbDTO:"+addReimbDTO);
        //如果其中属性为空或者''，返回错误信息
        if (proCode == null || proCode.isEmpty()) {
            return Result.fail("项目编号不能为空");
        }
        if (proName == null || proName.isEmpty()) {
            return Result.fail("项目名称不能为空");
        }
        if (reiMoney == null) {
            return Result.fail("报销金额不能为空");
        }
        if (reiReason == null || reiReason.isEmpty()) {
            return Result.fail("报销事由不能为空");
        }
        if (reiApplicantId == 0) {
            return Result.fail("报销人不能为空");
        }
        //获取年份和月份
        LocalDate now = LocalDate.now();
        String yearId = String.valueOf(now.getYear());
        String monthId = String.valueOf(now.getMonthValue());
        if(monthId.length()<2){
            monthId = "0"+monthId;
        }

        //根据年份和月份获取该会计区间的项目已经申报通过的金额，查看申报的金额石是否还能继续申报
        BigDecimal reiTotalPassedmoney = reimbMapper.getTotalPassedReiMoneyByYearMonthWithProCode(proCode, yearId, monthId);
        if (reiTotalPassedmoney == null) {
            reiTotalPassedmoney = BigDecimal.ZERO;
        }
        //获取项目的可报销金额
        BigDecimal effectivAmountMoneyByProCode = projectMapper.getEffectivAmountMoneyByProCode(proCode);
        //计算剩余可报销金额
        BigDecimal remainMoney= effectivAmountMoneyByProCode.subtract(reiTotalPassedmoney);
        System.out.println("最大金额："+effectivAmountMoneyByProCode.toString()+"剩余金额："+remainMoney.toString()+"申报金额："+reiMoney.toString()+"通过金额："+reiTotalPassedmoney.toString());
        //判断申报金额是否超过了可申报的最大额度
        if(reiMoney.compareTo(effectivAmountMoneyByProCode)>0||reiMoney.compareTo(remainMoney)>0){
            return Result.fail("申报金额超过可申报的最大额度，当前可申报最大额度为￥"+remainMoney+".");
        }

        // 数据验证通过后开始生成对应的需要的数据
        String reiOrderNum = OrderNumberGenerator.generateOrderNumber();

        // 设置属性
        reimb.setProCode(proCode);
        reimb.setProName(proName);
        reimb.setReiMoney(reiMoney);
        reimb.setReiReason(reiReason);
        reimb.setReiApplicantId(reiApplicantId);
        reimb.setReiOrderNum(reiOrderNum);
        reimb.setYearId(yearId);
        reimb.setMonthId(monthId);

        // 保存到数据库
        boolean save = save(reimb);
        if (save) {
            return Result.ok("申报提交成功，请等待审核");
        } else {
            return Result.fail("申报失败，请稍后重试");
        }
    }

    @Override
    public Result getReimbList() {
        return Result.ok(reimbMapper.getReimbList());
    }

    @Override
    public Result getReimbListByApplicantId(Integer userId) {
        return Result.ok(reimbMapper.getReimbListByApplicantId(userId));
    }

    @Override
    public Result updateStatusByVerifier(ReimbVerifyDTO reimbVerifyDTO) {
        //验证数据是否完整或者合法
        String reiOrderNum = reimbVerifyDTO.getReiOrderNum();
        Integer reiStatus = reimbVerifyDTO.getReiStatus();
        Integer reiVerifierId = reimbVerifyDTO.getReiVerifierId();
        if (reiOrderNum == null || reiOrderNum.isEmpty()) {
            return Result.fail("报销单号不能为空");
        }
        if (reiStatus == null&&!reiStatus.equals(2)&&!reiStatus.equals(3)) {
            return Result.fail("审核状态不合法");
        }
        if (reiVerifierId == null||reiVerifierId.equals(0)) {
            return Result.fail("审核人不能为空");
        }
        //根据报销单号获取对应的报销金额，并获取对应项目的剩余可用金额进行校验
        ReimbDTO reimb = reimbMapper.getReimbByOrderNum(reiOrderNum);
        //根据年份和月份获取该会计区间的项目已经申报通过的金额，查看申报的金额石是否还能继续申报
        BigDecimal reiTotalPassedmoney = reimbMapper.getTotalPassedReiMoneyByYearMonthWithProCode(reimb.getProCode(), reimb.getYearId(), reimb.getMonthId());
        if (reiTotalPassedmoney == null) {
            reiTotalPassedmoney = BigDecimal.ZERO;
        }
        BigDecimal effectivAmountMoneyByProCode = projectMapper.getEffectivAmountMoneyByProCode(reimb.getProCode());
        //计算剩余可报销金额
        BigDecimal remainMoney= effectivAmountMoneyByProCode.subtract(reiTotalPassedmoney);

        //判断申报金额是否超过了可申报的最大额度
        if(reimb.getReiMoney().compareTo(effectivAmountMoneyByProCode)>0||reimb.getReiMoney().compareTo(remainMoney)>0){
            return Result.fail("申报金额超过可申报的最大额度，当前可申报最大额度为￥"+remainMoney+".");
        }

        //更新数据库
        Integer i = reimbMapper.updateStatusByVerifier(reiOrderNum, reiStatus, reiVerifierId);
        if (i == 1) {
            return Result.ok("审核成功");
        }else {
            return Result.fail("审核失败，请稍后重试");
        }



    }

    @Override
    public Result getPendingReimbList() {
        return Result.ok(reimbMapper.getPendingReimbList());
    }

    @Override
    public Result getZzst() {
        return Result.ok(reimbMapper.getZzst());
    }
}
